﻿using System.Web.Mvc;
using MvcDependency.Internal;

namespace MvcDependency
{
    public static class HtmlExtensions
    {
        public static HtmlHelper RequiresJs(this HtmlHelper html, 
            string filePath, 
            string group = "Site",
            ushort order = 100)
        {
            if(filePath.StartsWith("~/"))
                filePath = new UrlHelper(html.ViewContext.RequestContext).Content(filePath);
            html.ViewContext.GetManager().Add(filePath, group, order, DependencyType.Javascript);
            return html;
        }

        public static MvcHtmlString RenderJs(this HtmlHelper html, string version, string group = "Site")
        {
            return MvcHtmlString.Create(html.ViewContext.GetManager()
                .Render(DependencyType.Javascript, group, null, version));
        }

        internal static DependencyManager GetManager(this ViewContext vc)
        {
            return DependencyManager.Get(vc.HttpContext);
        }
    }
}